<?php
class Admin_controller_category extends Core_controller_abstract
{
	protected function _init()
	{
		
	}
	
	public function index()
	{
		$cate = new Admin_model_category();
		$cates = $cate->getAll();
		
		$catesTree = '<ul><li>全部分类'.$cate->getTree().'</li></ul>';
		
		$this->view->assign('catesTree', $catesTree);
		$this->view->assign('cates', $cates);
		$this->view->assign('title', '商品分类管理');
	}
	
	public function add()
	{
		$cate = new Admin_model_category();
		
		if($this->requstMethod == 'POST')
		{
			$cate->add($_POST);
			$this->_redirect('admin.php?m=category');
		}
		
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		$cates = $cate->getAll();
		$this->view->assign('cates', $cates);
		$this->view->assign('id', $id);
		$this->view->assign('title', '商品分类管理');
	}

	public function modify()
	{
		$cate = new Admin_model_category();
		if($this->requstMethod == 'POST')
		{
			$cate->save($_POST);
			$this->_redirect('admin.php?m=category');
		}
		else if(isset($_GET['id']))
		{
			$cateInfo = $cate->getOne($_GET['id']);
			$where = "`cateKey` NOT LIKE '{$cateInfo['cateKey']}:{$cateInfo['id']}%' AND `id`!='{$cateInfo['id']}'";
			$cates = $cate->getAll($where);
			$this->view->assign('cates', $cates);
			$this->view->assign('title', "修改分类[{$cateInfo['cateName']}]");
			$this->view->assign('cateInfo', $cateInfo);
		}
	}
	public function delete()
	{
		$cate = new Admin_model_category();
		
		if($this->requstMethod == 'POST')
		{
			if(isset($_POST['Yes']))
			{
				if($cate->hasChildren($_POST['id']))
				{
					$this->view->setAutoDisplay(false);
					header("Content-type:text/html;charset=utf-8");
					echo "该分类还有子类，不能删除";
					$this->_redirect('admin.php?m=category', 3);
				}
				$cate->delete($_POST['id']);
				$this->_redirect('admin.php?m=category');
			}
			else
			{
				$this->_redirect('admin.php?m=category');
			}
		}
		else if(isset($_GET['id']))
		{
			if($cate->hasChildren($_GET['id']))
			{
				$this->view->setAutoDisplay(false);
				header("Content-type:text/html;charset=utf-8");
				echo "该分类还有子类，不能删除";
				$this->_redirect('admin.php?m=category', 3);
			}
			$cateInfo = $cate->getOne($_GET['id']);
			$this->view->assign('title', "确认删除分类[{$cateInfo['cateName']}]");
			$this->view->assign('cateInfo', $cateInfo);
		}
	}
}